{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 文件操作思路\n",
    "* 打开文件流\n",
    "* 读取文件流或者写入文件流\n",
    "* 关闭文件流\n",
    "### 模式追加文件\n",
    "* w：写入模式\n",
    "* r: 读模式\n",
    "* a：从末尾开始起\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 写入文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting temp.txt\n"
     ]
    }
   ],
   "source": [
    "%%writefile temp.txt\n",
    "111111111\n",
    "222222222\n",
    "333333333"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 打开文件\n",
    "* 注意read(size) 添加size目的由于内存爆了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11222\n"
     ]
    }
   ],
   "source": [
    "txt=open(\"./data/temp.txt\")\n",
    "txt_read=txt.read(5)\n",
    "print(txt_read)\n",
    "txt.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 如果想省事不考虑依次型内存读取我们可以采用readline()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "txt=open(\"./data/temp.txt\")\n",
    "lines=txt.readlines();"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['1122222222\\n', '23333333\\n', '33333333333333']\n"
     ]
    }
   ],
   "source": [
    "print(lines)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "current_list：1122222222\n",
      "\n",
      "current_list：23333333\n",
      "\n",
      "current_list：33333333333333\n"
     ]
    }
   ],
   "source": [
    "for item in lines :\n",
    "    print('current_list：'+item)\n",
    "txt.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "txt=open('write_temp.txt','w')\n",
    "txt.write('121212122\\n')\n",
    "txt.write('1212123郭超\\n')\n",
    "txt.write('guochao1212')\n",
    "txt.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 二次写入会将原来的全部覆盖"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "txt=open('write_temp.txt','w')\n",
    "txt.write('ggggg\\n')\n",
    "txt.write('cccccc\\n')\n",
    "txt.write('wwwwww')\n",
    "txt.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 追加写入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "txt=open('write_temp.txt','a')\n",
    "txt.write('cccc\\n')\n",
    "txt.write('ddd\\n')\n",
    "txt.write('zzzz')\n",
    "txt.close()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 既写又读"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 每做一次open 如果不关闭，流是共享一片内存"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "10\n",
      "11\n",
      "\n"
     ]
    }
   ],
   "source": [
    "txt=open('write_temp.txt','w')\n",
    "for i in range(12):\n",
    "    txt.write(str(i)+'\\n')\n",
    "txt.flush()\n",
    "txt1=open('write_temp.txt','r')\n",
    "print(txt1.read())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 注意一条原则打开一次后，必须关闭；"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "txt=open('write_temp.txt','w')\n",
    "for i in range(6):\n",
    "    txt.write(str(i)+'\\n')\n",
    "txt.close()    \n",
    "txt1=open('write_temp.txt','r')\n",
    "print(txt1.read())\n",
    "txt1.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 文件操作的标准写法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "txt=open('write_temp.txt','w')\n",
    "try:\n",
    "    for i in range(10):\n",
    "         temp=1/(i-50)\n",
    "         txt.write(str(temp)+'\\n')\n",
    "except Exception :  \n",
    "     print('Exception: {}异常了'+i)\n",
    "finally :\n",
    "     txt.close()\n",
    "        "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 你可以反复调用write()来写入文件，但是务必要调用f.close()来关闭文件。当我们写文件时，\n",
    "* 操作系统往往不会立刻把数据写入磁盘，而是放到内存缓存起来，空闲的时候再慢慢写入。\n",
    "* 只有调用close()方法时，操作系统才保证把没有写入的数据全部写入磁盘。忘记调用close()的后果是数据可能只写了一部分到磁盘，剩下的丢失了。所以，还是用with语句来得保险：\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('write_temp.txt','a') as f :\n",
    "    f.write(\"追加上很多东西\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 带有字符编码读取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-0.02\r\n",
      "-0.02040816326530612\r\n",
      "-0.020833333333333332\r\n",
      "-0.02127659574468085\r\n",
      "-0.021739130434782608\r\n",
      "-0.022222222222222223\r\n",
      "-0.022727272727272728\r\n",
      "-0.023255813953488372\r\n",
      "-0.023809523809523808\r\n",
      "-0.024390243902439025\r\n",
      "追加上很多东西\n"
     ]
    }
   ],
   "source": [
    "import codecs\n",
    "with codecs.open('write_temp.txt', 'r', 'gbk') as f:\n",
    "    value=f.read()\n",
    "print(value)\n",
    "    "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
